package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.message.*;
import com.dcy.znoa.domain.publiz.Message;
import com.dcy.znoa.service.publiz.MessageService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

/**
 * @author pengyu
 */
//消息基表 controller
@Controller
@RequestMapping("message")
public class MessageController extends BaseController {
    @Autowired
    MessageService messageService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectMessageModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
//        return messageService.selectListMapper(model);
        Integer messType = model.getMessType();
        Long createOperId = model.getCreateOperId();
        Long receiveOperId = model.getReceiveOperId();
        if (messType != null) {
            switch (messType) {
                case 2:
                    if (createOperId == null) {
                        return new JsonForList("404", "messType为2时createOperId不能为空");
                    }
                    break;
                case 3:
                    if (receiveOperId == null) {
                        return new JsonForList("404", "messType为3时receiveOperId不能为空");
                    }
                    break;
                default:
                    break;
            }
        }
        return successPage(messageService.selectListMapper(model));
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectListByType")
    public JsonForList selectListByType(@Valid SelectMessageModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
//        return messageService.selectListMapper(model);
        if (model.getReceiveOperId() == null || model.getMessType() == null) {
            return new JsonForList("404", "receiveOperId和messType不能为空");
        }
        return messageService.selectListByType(model);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectReadListByType")
    public JsonForList selectReadListByType(@Valid SelectMessageModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
//        return messageService.selectListMapper(model);
        if (model.getReceiveOperId() == null || model.getMessType() == null) {
            return new JsonForList("404", "receiveOperId和messType不能为空");
        }
        return messageService.selectReadListByType(model);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUnReadListByType")
    public JsonForList selectUnReadListByType(@Valid SelectMessageModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
//        return messageService.selectListMapper(model);
        if (model.getReceiveOperId() == null || model.getMessType() == null) {
            return new JsonForList("404", "receiveOperId和messType不能为空");
        }
        return messageService.selectUnReadListByType(model);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectGroupItemByOperId")//eg:我的申请几条，我的通知几条 一般是用于APP单人用，以后也可以给WEB用，如果有需求，那时要改model的NotNull
    public JsonCommon selectGroupItemByOperId(@Valid SelectMsgGroupModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
//        return messageService.selectListMapper(model);
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        return new JsonCommon(messageService.selectGroupItemByOperId(model));
    }


    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddMessageModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        Message cp = new Message();
        BeanUtils.copyProperties(cp, model);
        return messageService.insert(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateMessageModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Message cp = new Message();
        BeanUtils.copyProperties(cp, model);
        return messageService.updateById(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicDeleteById")
    public JsonCommon logicDeleteById(@Valid LogicDelMessageModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Message cp = new Message();
        BeanUtils.copyProperties(cp, model);
        return messageService.updateById(cp);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/queryEntityByEId")//Long entityId,String entityTbl,Integer type
    public JsonCommon queryEntityByEId(@Valid QueryEntityModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        model.setOperId(getCurrentUserId());
        return new JsonCommon(messageService.queryEntityByEId(model));
    }



}
